package com.mjl.shops.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.mjl.shops.R;
import com.mjl.shops.base.BaseActivity;
import com.mjl.shops.fragment.CollatorFragment;
import com.mjl.shops.fragment.HomeFragment;
import com.mjl.shops.fragment.MineFragment;
import com.mjl.shops.fragment.ShopCarFragment;
import com.mjl.shops.utils.CommUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    private static final int HOME = 0;

    private static final int COLLATOR = 1;

    private static final int SHOP = 2;

    private static final int MINE = 3;

    private Fragment mHomeFragment, mCollatorFragment, mShopFragment, mMineFragment;

    private Fragment[] mFragments = new Fragment[4];

    //底部按钮文本
    private TextView[] textViews;

    private ImageView[] iconViews;

    //当前fragment下标、下一个fragment下标
    private int mCutterIndex, mNextIndex;

    private long mCurrentTime = 0;

    @BindView(R.id.home_tv)
    TextView mHome_tv;

    @BindView(R.id.collator_tv)
    TextView mCollator_tv;

    @BindView(R.id.shop_tv)
    TextView mShop_tv;

    @BindView(R.id.mine_tv)
    TextView mMine_tv;

    @BindView(R.id.home_iv)
    ImageView mHome_iv;

    @BindView(R.id.collator_iv)
    ImageView mCollator_iv;

    @BindView(R.id.shop_iv)
    ImageView mShop_iv;

    @BindView(R.id.mine_iv)
    ImageView mMine_iv;

    @Override
    protected void setContentViewBefore() {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    protected int getViews() {
        return R.layout.activity_main;
    }

    @Override
    protected void setViews() {
        ButterKnife.bind(this);
        textViews = new TextView[]{mHome_tv, mCollator_tv, mShop_tv, mMine_tv};
        iconViews = new ImageView[]{mHome_iv, mCollator_iv, mShop_iv, mMine_iv};
    }

    @Override
    protected void setDate() {
        setDefFrament();
    }

    @Override
    protected String getStateColor() {
        return "";
    }

    @Override
    protected int getStetaBackgroundStyle() {
        return 2;
    }

    //设置默认展示的Fragment
    private void setDefFrament() {
        mCutterIndex = HOME;
        setSelectTextColor(mCutterIndex);
        mHomeFragment = new HomeFragment();
        mFragments[mCutterIndex] = mHomeFragment;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.main_fragment_fl, mHomeFragment, mHomeFragment.getClass().getName());
        transaction.commitAllowingStateLoss();
    }

    @OnClick({R.id.home_ll, R.id.collator_ll, R.id.shop_car_ll, R.id.mine_ll})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.home_ll:
                mNextIndex = HOME;
                break;
            case R.id.collator_ll:
                mNextIndex = COLLATOR;
                break;
            case R.id.shop_car_ll:
                mNextIndex = SHOP;
                break;
            case R.id.mine_ll:
                mNextIndex = MINE;
                break;
            default:
                break;
        }

        setSelectFragment();
    }

    //设置选择的fragment
    private void setSelectFragment() {
        if (mNextIndex == mCutterIndex) {
            //防止重复点击
            return;
        }
        changeFragment();
        setNotSelectTextColor(mCutterIndex);
        setSelectTextColor(mNextIndex);
        mCutterIndex = mNextIndex;
    }

    //切换fragment
    private void changeFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (mNextIndex) {
            case HOME://首页
                //判断当前的Fragment是否为空，不为空则隐藏
                if (null != mFragments[mCutterIndex]) {
                    transaction.hide(mFragments[mCutterIndex]);
                }
                //先根据Tag从FragmentTransaction事物获取之前添加的Fragment
                Fragment fragment1;
                if (mFragments[mNextIndex] == null) {
                    mHomeFragment = new HomeFragment();
                    mFragments[mNextIndex] = mHomeFragment;
                    fragment1 = mHomeFragment;
                } else {
                    fragment1 = getSupportFragmentManager().findFragmentByTag(mFragments[mNextIndex].getClass().getName());
                    if (null == fragment1) {
                        //如fragment为空，则之前未添加此Fragment。便从集合中取出
                        if (mFragments[mNextIndex] == null) {
                            mHomeFragment = new HomeFragment();
                            mFragments[mNextIndex] = mHomeFragment;
                        }
                        fragment1 = mHomeFragment;
                    }
                }
                //判断此Fragment是否已经添加到FragmentTransaction事物中
                if (!fragment1.isAdded()) {
                    transaction.add(R.id.main_fragment_fl, fragment1, fragment1.getClass().getName());
                } else {
                    transaction.show(fragment1);
                }
                transaction.commit();
                break;
            case COLLATOR:
                //判断当前的Fragment是否为空，不为空则隐藏
                if (null != mFragments[mCutterIndex]) {
                    transaction.hide(mFragments[mCutterIndex]);
                }
                Fragment fragment2;
                if (mFragments[mNextIndex] == null) {
                    mCollatorFragment = new CollatorFragment();
                    mFragments[mNextIndex] = mCollatorFragment;
                    fragment2 = mCollatorFragment;
                } else {
                    //先根据Tag从FragmentTransaction事物获取之前添加的Fragment
                    fragment2 = getSupportFragmentManager().findFragmentByTag(mFragments[mNextIndex].getClass().getName());
                    if (null == fragment2) {
                        //如fragment为空，则之前未添加此Fragment。便从集合中取出
                        if (mFragments[mNextIndex] == null) {
                            mCollatorFragment = new CollatorFragment();
                            mFragments[mNextIndex] = mCollatorFragment;
                        }
                        fragment2 = mCollatorFragment;
                    }
                }
                //判断此Fragment是否已经添加到FragmentTransaction事物中
                if (!fragment2.isAdded()) {
                    transaction.add(R.id.main_fragment_fl, fragment2, fragment2.getClass().getName());
                } else {
                    transaction.show(fragment2);
                }
                transaction.commit();
                break;
            case SHOP:
                //判断当前的Fragment是否为空，不为空则隐藏
                if (null != mFragments[mCutterIndex]) {
                    transaction.hide(mFragments[mCutterIndex]);
                }
                Fragment fragment3;
                if (mFragments[mNextIndex] == null) {
                    mShopFragment = new ShopCarFragment();
                    mFragments[mNextIndex] = mShopFragment;
                    fragment3 = mShopFragment;
                } else {
                    //先根据Tag从FragmentTransaction事物获取之前添加的Fragment
                    fragment3 = getSupportFragmentManager().findFragmentByTag(mFragments[mNextIndex].getClass().getName());
                    if (null == fragment3) {
                        //如fragment为空，则之前未添加此Fragment。便从集合中取出
                        if (mFragments[mNextIndex] == null) {
                            mShopFragment = new ShopCarFragment();
                            mFragments[mNextIndex] = mShopFragment;
                        }
                        fragment3 = mShopFragment;
                    }
                }
                //判断此Fragment是否已经添加到FragmentTransaction事物中
                if (!fragment3.isAdded()) {
                    transaction.add(R.id.main_fragment_fl, fragment3, fragment3.getClass().getName());
                } else {
                    transaction.show(fragment3);
                }
                transaction.commit();
                break;
            case MINE:
                //判断当前的Fragment是否为空，不为空则隐藏
                if (null != mFragments[mCutterIndex]) {
                    transaction.hide(mFragments[mCutterIndex]);
                }
                Fragment fragment4;
                if (mFragments[mNextIndex] == null) {
                    mMineFragment = new MineFragment();
                    mFragments[mNextIndex] = mMineFragment;
                    fragment4 = mMineFragment;
                } else {
                    //先根据Tag从FragmentTransaction事物获取之前添加的Fragment
                    fragment4 = getSupportFragmentManager().findFragmentByTag(mFragments[mNextIndex].getClass().getName());
                    if (null == fragment4) {
                        //如fragment为空，则之前未添加此Fragment。便从集合中取出
                        if (mFragments[mNextIndex] == null) {
                            mMineFragment = new MineFragment();
                            mFragments[mNextIndex] = mMineFragment;
                        }
                        fragment4 = mMineFragment;
                    }
                }
                //判断此Fragment是否已经添加到FragmentTransaction事物中
                if (!fragment4.isAdded()) {
                    transaction.add(R.id.main_fragment_fl, fragment4, fragment4.getClass().getName());
                } else {
                    transaction.show(fragment4);
                }
                transaction.commit();
                break;
        }
    }

    //设置选中的文本颜色
    private void setSelectTextColor(int index) {
        textViews[index].setTextColor(Color.parseColor("#1BBF80"));
        iconViews[index].setSelected(true);
    }

    //设置选中的文本颜色
    private void setNotSelectTextColor(int index) {
        textViews[index].setTextColor(Color.parseColor("#999999"));
        iconViews[index].setSelected(false);
    }

    public static void StratIntent(Context contexts){
        Intent intent = new Intent(contexts,MainActivity.class);
        contexts.startActivity(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((System.currentTimeMillis() - mCurrentTime)<2000){
            return super.onKeyDown(keyCode, event);
        }else{
            mCurrentTime = System.currentTimeMillis();
            CommUtils.setToast("再按一次退出程序");
            return true;
        }
    }
}
